package com.healthcloud.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.healthcloud.entity.AllergicHistory;
import com.healthcloud.entity.Disability;
import com.healthcloud.entity.ExposureHistory;
import com.healthcloud.entity.FamilyHistory;
import com.healthcloud.entity.GeneticHistory;
import com.healthcloud.entity.Insurance;
import com.healthcloud.entity.LivingEnvironment;
import com.healthcloud.entity.PastHistory;
import com.healthcloud.entity.Patient;
import com.healthcloud.service.AllergicHistoryService;
import com.healthcloud.service.DisabilityService;
import com.healthcloud.service.ExposureHistoryService;
import com.healthcloud.service.FamilyHistoryService;
import com.healthcloud.service.GeneticHistoryService;
import com.healthcloud.service.InsuranceService;
import com.healthcloud.service.LivingEnvironmentService;
import com.healthcloud.service.PastHistoryService;
import com.healthcloud.service.PatientService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.StringUtil;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by XYC on 2015/7/4.
 * @Controller
 * @ResponseBody 个人档安接口  for app  rocky add
 */
@ApiIgnore
@Api(value = "示例说明", description = "个人档安接口")
@RestController
@RequestMapping(value = "/app/dean/patient")
public class PatientAppController {
    @Autowired
    private PatientService patientService;

    @Autowired
    private FamilyHistoryService familyHistoryService;

    @Autowired
    private ExposureHistoryService exposureHistoryService;

    @Autowired
    private AllergicHistoryService allergicHistoryService;

    @Autowired
    private PastHistoryService pastHistoryService;

    @Autowired
    private GeneticHistoryService geneticHistoryService;

    @Autowired
    private DisabilityService disabilityService;

    @Autowired
    private InsuranceService insuranceService;

    @Autowired
    private LivingEnvironmentService livingEnvironmentService;
    /**
     * 保存或者更新病人基础资料 (该类先不支持修改用户信息)
     * @param patient
     * @return
     */
	@ApiOperation(value = "保存或者更新病人基础资料 (该类先不支持修改用户信息)",notes ="保存或者更新病人基础资料 (该类先不支持修改用户信息)")
//    @RequestMapping(value = "/archives/Save", method = RequestMethod.POST)
    @RequestMapping(value = "/archives/Set", method = RequestMethod.POST)
    public Object saveOrUpdatePatientArchive(Patient patient) {
//        try {
//            patient.setNegativeEvents(new String(patient.getNegativeEvents().toString().getBytes("UTF-8")));
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
   /*     switch (oper){
            case "add": Patient patientTemp = patientService.findByIdCard(patient.getIdCard());
        }*/
 /*
        if (oper!=null && oper.compareTo("add")==0){
            if( patientService.findByIdCard(patient.getIdCard())!=null){
                msg=new JsonMsg("该身份证已被注册");
                msg.setSuccess(false);
                return msg;
            }
        }*/
    	//设置身份证号码小写   20170215 lixx
    	String idCard = patient.getIdCard();
    	if(!"".equals(StringUtil.stringToNotNull(idCard))){
    		idCard = idCard.toLowerCase();
    	}
        ApiRes msg;
        Patient patientTemp = patientService.findByIdCard(idCard);
        if(patientTemp!=null && patientTemp.getUserId()!=null&&patientTemp.getUserId()!=patient.getUserId()){
            msg=new ApiRes(false,"","","该身份证已被注册");
            /*msg.setSuccess(false);
            msg.setMsg("该身份证已被注册");*/
            return msg;
        }
        patient.setIdCard(idCard);
        patient = patientService.saveOrUpdate(patient);
        patientService.DataTimeToString(patient);
        msg = new ApiRes(patient);
        return msg;
    }
    @RequestMapping(value = "/archives/SetNew", method = RequestMethod.POST)
    public Object saveOrUpdatePatientArchiveNew(Patient patient) {
//        try {
//            patient.setNegativeEvents(new String(patient.getNegativeEvents().toString().getBytes("UTF-8")));
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
   /*     switch (oper){
            case "add": Patient patientTemp = patientService.findByIdCard(patient.getIdCard());
        }*/
 /*
        if (oper!=null && oper.compareTo("add")==0){
            if( patientService.findByIdCard(patient.getIdCard())!=null){
                msg=new JsonMsg("该身份证已被注册");
                msg.setSuccess(false);
                return msg;
            }
        }*/
    	//设置身份证号码小写   20170215 lixx
    	String idCard = patient.getIdCard();
    	if(!"".equals(StringUtil.stringToNotNull(idCard))){
    		idCard = idCard.toLowerCase();
    	}
        ApiRes msg;
        Patient patientTemp = patientService.findByIdCard(idCard);
        if(patientTemp!=null && patientTemp.getUserId()!=null&&patientTemp.getUserId()!=patient.getUserId()){
            msg=new ApiRes(false,"","","该身份证已被注册");
            /*msg.setSuccess(false);
            msg.setMsg("该身份证已被注册");*/
            return msg;
        }
        patient.setIdCard(idCard);
        patient = patientService.saveOrUpdate(patient);
        patientService.DataTimeToString(patient);
        msg = new ApiRes(patient);
        return msg;
    }
    @RequestMapping(value = "/archives/Update", method = RequestMethod.POST)
    public Object updatePatientArchive(Patient patient) {
    	//设置身份证号码小写   20170215 lixx
    	String idCard = patient.getIdCard();
    	if(!"".equals(StringUtil.stringToNotNull(idCard))){
    		idCard = idCard.toLowerCase();
    	}
        ApiRes msg;
        patient.setIdCard(idCard);
        patient = patientService.saveOrUpdate(patient);
        patientService.DataTimeToString(patient);
        msg = new ApiRes(patient);
        return msg;
    }

    /**
     * 获取病人基础信息
     * @param idCard 身份证号码
     * @return
     */
	@ApiOperation(value = "获取病人基础信息",notes ="获取病人基础信息")
    @RequestMapping(value = "/archives/Get", method = RequestMethod.POST)
    public Object getPatientArchive(String idCard) {

        Patient patient = patientService.findByIdCard(idCard);
        patientService.DataTimeToString(patient);
        
        if(patient.getHeight()==0.0)
        {
        	patient.setHeight(170.0);
        }
        
        return new ApiRes(patient);
    }

    /**
     * 获取病人基础信息
     * @param id 病人主键id
     * @return
     */
    @RequestMapping(value = "/archives/GetById", method = RequestMethod.POST)
    public Object getPatientArchiveById(Long id) {
        Patient patient = patientService.getById(id);
        patientService.DataTimeToString(patient);

        if(patient.getHeight()==0.0)
        {
        	patient.setHeight(170.0);
        }

        return new ApiRes(patient);
    }

    /**
     * 设置病人家族史相关信息
     * @param familyHistory
     * @return
     */
	@ApiOperation(value = "设置病人家族史相关信息",notes ="设置病人家族史相关信息")
    @RequestMapping(value = "/familyHistory/Set", method = RequestMethod.POST)
    public  Object saveOrUpdatePatientFamilyHistory(FamilyHistory familyHistory){
       FamilyHistory familyHistory1 = familyHistoryService.saveOrUpData(familyHistory);
        return new ApiRes(familyHistory1);
    }

    /**
     * 获取病人家族史相关信息
     * @param familyHistorie
     * @return
     */
	@ApiOperation(value = "获取病人家族史相关信息",notes ="获取病人家族史相关信息")
    @RequestMapping(value = "/familyHistory/Get", method = RequestMethod.POST)
    public  Object getPatientFamilyHistory(FamilyHistory familyHistorie){
        ArrayList<FamilyHistory> familyHistories = familyHistoryService.findByIdCard(familyHistorie);
//        familyHistoryService
        return new ApiRes(familyHistories);
    }

    /**
     *添加暴露史信息
     * @param exposureHistory
     * @return
     */
	@ApiOperation(value = "添加暴露史信息",notes ="添加暴露史信息")
    @RequestMapping(value = "/exposureHistory/Set", method = RequestMethod.POST)
    public Object SaveOrUpdataExposureHistory(ExposureHistory exposureHistory){
//        ExposureHistory exposureHistory =
        ExposureHistory exposureHistory1 = exposureHistoryService.saveOrUpdata(exposureHistory);
        return new ApiRes(exposureHistory1);
//        return null;
    }

    /**
     * 获取暴露史信息
     * @param exposureHistory
     * @return
     */
	@ApiOperation(value = "获取暴露史信息",notes ="获取暴露史信息")
    @RequestMapping(value = "/exposureHistory/Get", method = RequestMethod.POST)
    public Object GetExposureHistory(ExposureHistory exposureHistory){
        ExposureHistory exposureHistory1 = exposureHistoryService.findByEntity(exposureHistory);
        return new ApiRes(exposureHistory1);
    }

    /**
     * 设置过敏史相关内容
     * @param allergicHistory
     * @return
     */
    @Deprecated
    @RequestMapping(value = "/allergicHistory/Set", method = RequestMethod.POST)
    public Object SaveOrUpdataAllergicHistory(AllergicHistory allergicHistory){
        AllergicHistory allergicHistory1 =allergicHistoryService.saveOrUpdata(allergicHistory, false);
        return new ApiRes(allergicHistory1);
    }

    /**
     * 设置过敏史相关内容List操作
     * @return
     */
	@ApiOperation(value = "设置过敏史相关内容List操作",notes ="设置过敏史相关内容List操作")
    @RequestMapping(value = "/allergicHistoryList/Set", method = RequestMethod.POST)
    public Object SaveOrUpdataAllergicHistoryList(@ModelAttribute AllergicHistory allergicHistory){
        Boolean bool = allergicHistoryService.saveEntityList(allergicHistory.getAllList());
        ApiRes apiRes = new ApiRes(null);
        return apiRes;
    }

    /**
     * 获取用户过敏史相关信息
     * @param allergicHistory
     * @return
     */
	@ApiOperation(value = "获取用户过敏史相关信息",notes ="获取用户过敏史相关信息")
    @RequestMapping(value = "/allergicHistory/Get", method = RequestMethod.POST)
    public Object GetAllergicHistory(AllergicHistory allergicHistory){
        ArrayList<AllergicHistory> allergicHistories =allergicHistoryService.findByEntity(allergicHistory);
        return new ApiRes(allergicHistories);
    }

    /**
     * 设置既往史相关内容List操作
     * @param pastHistory
     * @return
     */
	@ApiOperation(value = "设置既往史相关内容List操作",notes ="设置既往史相关内容List操作")
    @RequestMapping(value = "/pastHistoryList/Set", method = RequestMethod.POST)
    public Object SetPastHistory(PastHistory pastHistory){
        Boolean bool = pastHistoryService.saveList(pastHistory.getAllList());
        ApiRes apiRes = new ApiRes(null);
        apiRes.setSuccess(bool);
        return apiRes;
    }

    /**
     * 获取用户既往史相关信息
     * @param pastHistory
     * @return
     */
	@ApiOperation(value = "获取用户既往史相关信息",notes ="获取用户既往史相关信息")
    @RequestMapping(value = "/pastHistory/Get", method = RequestMethod.POST)
    public Object GetPastHistory(PastHistory pastHistory){
        return new ApiRes(pastHistoryService.findByIdCard(pastHistory));
    }

    /**
     * 设置遗传病史相关内容
     * @param geneticHistory
     * @return
     */
	@ApiOperation(value = "设置遗传病史相关内容",notes ="设置遗传病史相关内容")
    @RequestMapping(value = "/geneticHistory/Set", method = RequestMethod.POST)
    public Object SetGeneticHistory(GeneticHistory geneticHistory){
        GeneticHistory geneticHistory1 = geneticHistoryService.saveOrUpdata(geneticHistory);
        return new ApiRes(geneticHistory1);
    }

    /**
     * 获取用户遗传病史相关内容
     * @param geneticHistory
     * @return
     */
	@ApiOperation(value = "获取用户遗传病史相关内容",notes ="获取用户遗传病史相关内容")
    @RequestMapping(value = "/geneticHistory/Get", method = RequestMethod.POST)
    public Object GetGeneticHistory(GeneticHistory geneticHistory){
        ArrayList<GeneticHistory> geneticHistories = geneticHistoryService.findByIdCard(geneticHistory);
        return new ApiRes(geneticHistories);
    }

    /**
     * 设置用户残疾情况相关数据
     * @param disability
     * @return
     */
	@ApiOperation(value = "设置用户残疾情况相关数据",notes ="设置用户残疾情况相关数据")
    @RequestMapping(value = "/disability/Set", method = RequestMethod.POST)
    public Object SetDisability(Disability disability){
        Disability disability1 = disabilityService.saveOrUpdata(disability);
        return new ApiRes(disability1);
    }

    /**
     * 获取用户残疾情况
     * @param disability
     * @return
     */
	@ApiOperation(value = "获取用户残疾情况",notes ="获取用户残疾情况")
    @RequestMapping(value = "/disability/Get", method = RequestMethod.POST)
    public Object GetDisability(Disability disability){
        ArrayList<Disability> disabilities = disabilityService.findByIdCard(disability);
        return new ApiRes(disabilities);
    }

    /**
     * 设置用户保险相关信息
     * @param insurance
     * @return
     */
	@ApiOperation(value = "设置用户保险相关信息",notes ="设置用户保险相关信息")
    @RequestMapping(value = "/insurance/Set", method = RequestMethod.POST)
    public Object SetInsurance(Insurance insurance){
        Insurance insurance1 = insuranceService.saveOrUpdata(insurance);
        return new ApiRes(insurance1);
    }

    /**
     * 获取用户相关保险信息
     * @param insurance
     * @return
     */
	@ApiOperation(value = "获取用户相关保险信息",notes ="获取用户相关保险信息")
    @RequestMapping(value = "/insurance/Get", method = RequestMethod.POST)
    public Object GetInsurance(Insurance insurance){
        ArrayList<Insurance> insurances = insuranceService.findByIdCard(insurance);
        return new ApiRes(insurances);
    }

    
    /**
     * 设置用户居住环境相关信息
     * @param livingenvironment
     * @return
     */
    @RequestMapping(value = "/livingenvironment/Set", method = RequestMethod.POST)
    public Object SetLivingEnvironment(LivingEnvironment livingenvironment){
    	LivingEnvironment livingenvironment1 = livingEnvironmentService.saveOrUpdata(livingenvironment);
        return new ApiRes(livingenvironment1);
    }

    /**
     * 获取用户相关居住环境信息
     * @param idCard
     * @return
     */
    @RequestMapping(value = "/livingenvironment/Get", method = RequestMethod.POST)
    public Object GetLivingEnvironment(String idCard){
        ArrayList<LivingEnvironment> livingenvironments = livingEnvironmentService.findByIdCard(idCard);
        return new ApiRes(livingenvironments);
    }



    /**
     * 上传病人档案头像
     *
     * @param patientId    病人档案id
     * @param userImage 图片base64编码
     * @return
     */
    @RequestMapping(value = "/uploadPatientImage", method = RequestMethod.POST)
    public Object uploadPatientImage(Long patientId, String userImage) {
        //数据非空验证
        if (patientId == null) {
            return new ApiRes(false, null, "patientId:" + patientId, "病人档案id为空！");
        }
        if (StringUtils.isBlank(userImage)) {
            return new ApiRes(false, null, "userImage:" + userImage, "头像图片为空！");
        }
        Patient patient = patientService.getById(patientId);
        patient.setUserImage(userImage);
        patientService.saveOrUpdate(patient);
        return new ApiRes(true, null, null, "上传头像成功！");
    }

}
